package com.adtech.farmProducts.blue.adapter;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.adtech.farmProducts.R;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by wuyicheng on 2017/9/2.
 */

public class ListViewAdapter extends ArrayAdapter<Event> {

    private int resourceId;



    public ListViewAdapter(Context context,int textViewResourceId ,List<Event> objects) {
        super(context, textViewResourceId,objects);
        resourceId = textViewResourceId;

    }

   /* @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Event event = getItem(position); // 获取当前项的Fruit实例
        View view;
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            TextView name = (TextView) view.findViewById (R.id.lv_name_listviewadapter);
            name.setText(event.getName());
        return view;
    }*/


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Event event = getItem(position); // 获取当前项的Fruit实例
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) view.findViewById (R.id.lv_name_listviewadapter);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }
        viewHolder.name.setText(new Gson().toJson(event));
        return view;
    }

    class ViewHolder {


        TextView name;

    }
}
